package com.game.pacman.client;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.LinkedList;

public class Monster  {
	
	private Image image;//표시될 이미지
	private int x, y;//위치 정보
	private int dx, dy;//이동량
	private int direction; //이동방향
	private boolean active; //활동상태(T), 약화상태(F)
	private boolean crashWall; //충돌상태(T)
	private boolean crashPlayer;//충돌상태(T)
	
	public Monster(Image image,int x,int y,int direction) {
		this.image = image;
		this.x = x;
		this.y = y;
		this.direction=direction;
		this.active=true;
		this.crashWall=false;
		this.crashPlayer=false;
	}

	public void draw(Graphics g) {
		g.drawImage(image, x, y, null);
	}
	
	public void update() {

		x += dx;
		y += dy;
		
		//경계선_좌측
		if (x >= Constants.BUTTON_WIDTH){	
			x-=Constants.OBJECT_WIDTH;
		}
		
		//경계선_위측
		if (y >= 0) {
				y-=Constants.OBJECT_HEIGHT;
		}	
		
		//경계선_우측
		if (x <= Constants.WINDOW_WIDTH - Constants.OBJECT_WIDTH*2 -Constants.BUTTON_WIDTH ){
			x+=Constants.OBJECT_WIDTH;
		}
		//경계선_아래측
		if (y <= Constants.MAP_HEIGHT -Constants.OBJECT_HEIGHT*2){
				y+=Constants.OBJECT_HEIGHT;
		}
	}
	
	public boolean findLoad(PlayMapScreen map){
		//만약 벽이 있다면
		//새로운 방향 찾기
		//현위치에서 위쪽, 오른쪽,아래쪽, 왼쪽에 벽과 일치하는 데이터가 있는지 찾아보고 벽과 일치하지않는 데이터 저장
		//벽과 일치하지 않는데이터와 현위치의 진해방향중을 고려하여 갈수 있는 길 찾기.
		boolean crash=false;
		ArrayList<int[][]> wall=map.getWallList(); 
		ArrayList<Point> crashWalls =new ArrayList<Point>();
	//	loop:for(int i=0;i<wall.length;i++){
	//		Point crashWall = new Point();
			//위쪽방향 check
	//		switch(direction){
	//			case Constants.UP:
	//				if ((x-Constants.BUTTON_WIDTH)/Constants.OBJECT_WIDTH ==wall[i][0] && 
	//						(y/Constants.OBJECT_HEIGHT)-1 ==wall[i][1]){
	//					System.out.println("충돌:"+i+",x:"+wall[i][0]+",y:"+wall[i][1]+",진행방향:"+direction);
	//					moveStop();
	//					crashWall.x=wall[i][0];crashWall.y=wall[i][1];
//						crashWalls.add(crashWall);
						//새로운 방향설정
//						if ((x-Constants.BUTTON_WIDTH)/Constants.OBJECT_WIDTH-1 ==wall[i][0] && 
//								(y/Constants.OBJECT_HEIGHT)==wall[i][1]){
//						direction=(int)(Math.random()*2)+1;//Constants.LEFT;
//						}
//					}
//					break;
//				case Constants.LEFT:
//					if ((x-Constants.BUTTON_WIDTH)/Constants.OBJECT_WIDTH-1 ==wall[i][0] && 
//						(y/Constants.OBJECT_HEIGHT) ==wall[i][1]){
//							System.out.println("충돌:"+i+",x:"+wall[i][0]+",y:"+wall[i][1]+",진행방향:"+direction);
//							moveStop();
//							crashWalls.add(wall[i][1]);
//							//새로운 방향설정
//							direction=Constants.UP;
//							break loop;
//					}
//					break;
//				case Constants.RIGHT:
//					break;
//				case Constants.DOWN:
//					break;
					
//			}
			
//		}
	
//		//새로운 방향설정
//		direction=Constants.LEFT;
//		
//		for(int crashWall : crashWalls)
//		{
//			System.out.print(crashWall+",");
//		}
//		System.out.println();
		return crash;
//		if(x<=0 || x>=Constants.MAP_WIDTH+Constants.OBJECT_WIDTH||
//		   y<=0|| y>=Constants.MAP_HEIGHT){
//			System.out.println("경계선:"+x+","+y); //<-경계선에 도달했음 확인함.
//			//새로운 방향설정
//			
//		}
		
//		//충돌 체크 1. Monster와 Wall 충돌 체크
//
//		monsterRect.x = this.x + 5;
//		monsterRect.y = this.y+ 5;
//		monsterRect.width = Constants.OBJECT_WIDTH - 10;
//		monsterRect.height = Constants.OBJECT_HEIGHT - 10;
//		wallRect.x = playMapScreen.getX() + 5;
//		wallRect.y = player.getY() + 5;
//		wallRect.width = player.getWidth() - 10;
//		wallRect.height = player.getHeight() - 10;
//		
//			if (monsterRect.intersects(wallRect)) {
//

				
			}
//		}
//		//this.direction=1;
//	}
	
	public void moveStart(){
//		do{	
			switch (direction) {
				case Constants.LEFT:
					//if (x > 0)	x-=10;
					dx = -25;
					break;
				case Constants.RIGHT:
					//if (x <= Constants.WINDOW_WIDTH - image.getWidth(null) - 10)
					//	x+=10;
					dx = 25;
					break;
				case Constants.UP:
					//if (y > 0) y-=10;
					dy = -25;
					break;
				case Constants.DOWN:
				//	if (y <= Constants.WINDOW_HEIGHT - image.getHeight(null) - 10)
				//		y+=10;
					dy = 25;
					break;
					
			}
			 
//			if (x+dx < 0 || x+dx+Constants.OBJECT_WIDTH>Constants.WINDOW_WIDTH || y+dy<0 || y+dy+Constants.OBJECT_HEIGHT>Constants.WINDOW_HEIGHT) 
//				setDirection();
//			
//			System.out.println(dx);
//		}while((x+dx > 0 && x+dx+Constants.OBJECT_WIDTH<Constants.WINDOW_WIDTH) && (y+dy>0 && y+dy+Constants.OBJECT_HEIGHT<Constants.WINDOW_HEIGHT));
	}

	public void moveStop(){
		switch (direction) {
			case Constants.LEFT:
			case Constants.RIGHT:
				dx = 0;
				break;
			case Constants.UP:
			case Constants.DOWN:
				dy = 0;
				break;
		}
	}
	
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public int getWidth() {
		return image.getWidth(null);
	}
	public int getHeight() {
		return image.getHeight(null);
	}

	public int getDirection() {
		return direction;
	}

	public void setDirection() {
		this.direction = (int)(Math.random()*4)+1;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public boolean isCrashWall() {
		boolean CrashWall=false;
//		CrashWall=true;
		return CrashWall;
	}

	public void setCrashWall(boolean crashWall) {
		this.crashWall = crashWall;
	}

	public boolean isCrashPlayer() {
		boolean crashPlayer=false;
		//crashPlayer=true;
		return crashPlayer;
	}

	public void setCrashPlayer(boolean crashPlayer) {
		this.crashPlayer = crashPlayer;
	}
	
}



